package org.dromara.crawler.config;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

/**
 * @author Shup
 * @date 2025/6/30 17:40
 */
@RequiredArgsConstructor
@Configuration
public class GridFsConfig {

    private final GridFsProperties props;

    @Bean(name = "xinHuaGridFsTemplate")
    public GridFsTemplate xinHuaGridFsTemplate(MappingMongoConverter mappingMongoConverter) {
        MongoClient mongoClient = MongoClients.create(props.getUri());
        MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(mongoClient, props.getDatabase());
        return new GridFsTemplate(factory, mappingMongoConverter, "XinHua_bucket");
    }

    @Bean(name = "cnnGridFsTemplate")
    public GridFsTemplate cnnGridFsTemplate(MappingMongoConverter mappingMongoConverter) {
        MongoClient mongoClient = MongoClients.create(props.getUri());
        MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(mongoClient, props.getDatabase());
        return new GridFsTemplate(factory, mappingMongoConverter, "CNN_bucket");
    }

    @Bean(name = "wikiGridFsTemplate")
    public GridFsTemplate wikiGridFsTemplate(MappingMongoConverter mappingMongoConverter) {
        MongoClient mongoClient = MongoClients.create(props.getUri());
        MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(mongoClient, props.getDatabase());
        return new GridFsTemplate(factory, mappingMongoConverter, "Wiki_bucket");
    }

    @Bean(name = "knowledgeTargetGridFsTemplate")
    public GridFsTemplate knowledgeTargetGridFsTemplate(MappingMongoConverter mappingMongoConverter) {
        MongoClient mongoClient = MongoClients.create(props.getUri());
        MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(mongoClient, props.getDatabase());
        return new GridFsTemplate(factory, mappingMongoConverter, "knowledge_image");
    }

    @Bean(name = "hotpotGridFsTemplate")
    public GridFsTemplate hotpotGridFsTemplate(MappingMongoConverter mappingMongoConverter) {
        MongoClient mongoClient = MongoClients.create(props.getUri());
        MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(mongoClient, props.getDatabase());
        return new GridFsTemplate(factory, mappingMongoConverter, "fs_hostpot");
    }

}
